/**
 * Copyright (c) 2024-2025 National Fundamental Software of China Co., Ltd.
 *
 * You should have find a copy of NFSChina License. If not, please contact us
 * by email <os_support@nfschina>
 *
**/

/*
    SPDX-FileCopyrightText: 2011 Martin Gräßlin <mgraesslin@kde.org>
    SPDX-FileCopyrightText: 2012 Gregor Taetzner <gregor@freenet.de>
    SPDX-FileCopyrightText: 2012 Marco Martin <mart@kde.org>
    SPDX-FileCopyrightText: 2013 2014 David Edmundson <davidedmundson@kde.org>
    SPDX-FileCopyrightText: 2014 Sebastian Kügler <sebas@kde.org>
    SPDX-FileCopyrightText: 2021 Mikel Johnson <mikel5764@gmail.com>
    SPDX-FileCopyrightText: 2021 Noah Davis <noahadvs@gmail.com>

    SPDX-License-Identifier: GPL-2.0-or-later
*/
import QtQuick 2.15
import QtQuick.Templates 2.15 as T
import QtQuick.Layouts 1.15
import QtQml 2.15
import org.kde.plasma.plasmoid 2.0
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.extras 2.0 as PlasmaExtras
import org.kde.plasma.private.kicker 0.1 as Kicker
import org.kde.kirigami 2.12 as Kirigami

import org.kde.plasma.private.shell 2.0

EmptyPage {
    id: root
    property real scaleFactor: PlasmaCore.Units.devicePixelRatio
    property QtObject containment
    property bool missQueryFinished: false
    // plasmoid.rootItem is Kickoff.qml
    leftPadding: 0
    rightPadding: 0
    topPadding: 0
    bottomPadding: plasmoid.rootItem.backgroundMetrics.bottomPadding * 3 * scaleFactor
    readonly property var appletInterface: Plasmoid.self

    Layout.minimumWidth: (420 ) * scaleFactor
    Layout.maximumWidth: (420 ) * scaleFactor
    Layout.minimumHeight: (680 ) * scaleFactor
    Layout.maximumHeight: (680 ) * scaleFactor
    property alias normalPage: normalPage

    Kicker.ContainmentInterface {
        id: containmentInterface
    }

    contentItem: RowLayout {
        SideBar {
            id: sideBarSettings
            width: 49 * scaleFactor
            height: rightContent.height
            anchors {
                left: parent.left
                top: parent.top
                bottom: parent.bottom
            }
        }

        Rectangle {
            id: separator
            anchors {
                left: sideBarSettings.right
                top: parent.top
                topMargin: -topPadding
                bottom: parent.bottom
                bottomMargin: -bottomPadding
            }

            width: 1 * scaleFactor
            opacity: 0.1

            function updateBgEffect() {
                separator.color = PlasmaCore.Theme.globalIconTheme() !== "breeze-dark" ? "#222222" : "#CCCCCC"
            }

            Component.onCompleted: {
                updateBgEffect();
                PlasmaCore.Theme.themeChanged.connect(updateBgEffect);
            }
        }

        ColumnLayout {
            id: rightContent
            anchors {
                left: separator.right
                top: parent.top
                topMargin: 12 * scaleFactor
                bottom: parent.bottom
                right: parent.right
            }

            Search {
                id: search
                anchors {
                    left: rightContent.left
                    leftMargin: 12 * scaleFactor
                    right:rightContent.right
                    rightMargin: 12 * scaleFactor
                    top: parent.top
                }
            }
            VerticalStackView {
                id: contentItemStackView
                anchors {
                    left: parent.left
                    top: search.bottom
                    bottom: parent.bottom
                    right: parent.right
                    topMargin: 24 * scaleFactor
                    //rightMargin: 12
                    //leftMargin: 12
                }
                focus: true
                movementTransitionsEnabled: true
                // Not using a component to prevent it from being destroyed
                initialItem: NormalPage {
                    id: normalPage
                    anchors.fill: parent
                    objectName: "normalPage"
                }

                Component {
                    id: searchViewComponent

                    KickoffListView {
                        id: searchView
                        objectName: "searchView"
                        mainContentView: true
                        // Forces the function be re-run every time runnerModel.count changes.
                        // This is absolutely necessary to make the search view work reliably.
                        model: plasmoid.rootItem.runnerModel.count ? plasmoid.rootItem.runnerModel.modelForRow(0) : null
                        delegate: KickoffListDelegate {
                            width: view.availableWidth
                            isSearchResult: true

                            height: Math.ceil(48 * scaleFactor)
                            leftPadding: 18 * scaleFactor
                        }
                        activeFocusOnTab: true
                        // always focus the first item in the header focus chain
                        KeyNavigation.tab: root.header.nextItemInFocusChain()
                        T.StackView.onActivated: {
                            plasmoid.rootItem.sideBar = null
                            plasmoid.rootItem.contentArea = searchView
                        }

                        Loader {
                            id: phloader
                            anchors.centerIn: searchView.view
                            width: searchView.view.width - (PlasmaCore.Units.largeSpacing * 4)

                            active: searchView.view.count === 0
                            visible: active
                            asynchronous: true

                            sourceComponent: PlasmaExtras.PlaceholderMessage {
                                id: emptyHint

                                iconName: "edit-none"
                                opacity: curopacity
                                text: i18nc("@info:status", "No matched result")

                                Connections {
                                    target: Plasmoid.rootItem.runnerModel
                                    function onQueryFinished() {
                                        showAnimation.restart()
                                    }
                                }
                                Component.onCompleted: {
                                    if(missQueryFinished){
                                        showAnimation.restart()
                                        missQueryFinished = false
                                    }
                                }

                                NumberAnimation {
                                    id: showAnimation
                                    duration: PlasmaCore.Units.longDuration
                                    easing.type: Easing.OutCubic
                                    property: "opacity"
                                    target: emptyHint
                                    to: 1
                                }
                            }
                        }
                    }
                }

                Keys.priority: Keys.AfterItem
                // This is here rather than root because events are implicitly forwarded
                // to parent items. Don't want to send multiple events to searchField.
                Keys.forwardTo: plasmoid.rootItem.searchField

                Connections {
                    target: search
                    function onSearchTextChanged() {
                        missQueryFinished =false
                        if (search.searchText.length === 0 && contentItemStackView.currentItem.objectName !== "normalPage") {
                            contentItemStackView.reverseTransitions = true
                            contentItemStackView.replace(normalPage)
                        } else if (search.searchText.length > 0 && contentItemStackView.currentItem.objectName !== "searchView") {
                            contentItemStackView.reverseTransitions = false
                            contentItemStackView.replace(searchViewComponent)
                        }
                    }
                }

                Connections {
                    target: normalPage
                    function onPageChanged() {
                        if (contentItemStackView.currentItem.objectName !== "normalPage") {
                            contentItemStackView.reverseTransitions = true
                            contentItemStackView.replace(normalPage)
                        }
                    }
                }
            }
        }
    }

    Connections {
        target: sideBarSettings
        function onCurrentShowAppsPageChanged() {
            normalPage.toggle_contentItem(sideBarSettings.currentShowAppsPage)
        }
    }
    Connections {
        target: Plasmoid.rootItem.runnerModel
        function onQueryFinished() {
            if(!missQueryFinished){
                missQueryFinished=true;
            }
        }
    }

    Component.onCompleted: {
        rootModel.refresh();
    }
}
